package com.im.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.im.common.constant.Constant;
import com.im.common.enums.MessageTypeEnum;
import com.im.common.enums.WsEnum;
import com.im.common.util.StringTool;
import com.im.project.entity.Chat;
import com.im.project.entity.dto.DeleteChatDto;
import com.im.project.entity.dto.UpdateChatUserMessageDto;
import com.im.project.entity.vo.DeleteChatUserVo;
import com.im.project.entity.vo.MessageVo;
import com.im.project.mapper.ChatMapper;
import com.im.project.rabbitmq.RabbitmqProducer;
import com.im.project.service.ChatService;
import com.im.project.webSocket.UserChannelCtxMap;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 会话 服务层接口实现类
 *
 * @author yunZhouJiaDi
 */
@Slf4j
@Service
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements ChatService {

    @Resource
    private ChatMapper chatMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private RabbitmqProducer rabbitmqProducer;

    @Override
    public void updateChat(UpdateChatUserMessageDto updateChatUserMessageDto) {
        Chat chat = new Chat().setLastReceiveTime(updateChatUserMessageDto.getCurrentTime());
        if (updateChatUserMessageDto.getMessageType().equalsIgnoreCase(MessageTypeEnum.COMMON.getName()) || updateChatUserMessageDto.getMessageType().equalsIgnoreCase(MessageTypeEnum.WITHDRAW.getName())) {
            chat.setLastSendName("");
        } else {
            chat.setLastSendName(updateChatUserMessageDto.getLastSendName());
        }
        if (updateChatUserMessageDto.getMessageType().equalsIgnoreCase(MessageTypeEnum.TEXT.getName())) {
            chat.setLastMessage(StringTool.escape(updateChatUserMessageDto.getMessageContent()));
        } else if (updateChatUserMessageDto.getMessageType().equalsIgnoreCase(MessageTypeEnum.IMAGE.getName())) {
            chat.setLastMessage("[" + MessageTypeEnum.IMAGE.getMessage() + "]");
        } else if (updateChatUserMessageDto.getMessageType().equalsIgnoreCase(MessageTypeEnum.VIDEO.getName())) {
            chat.setLastMessage("[" + MessageTypeEnum.VIDEO.getMessage() + "]");
        } else if (updateChatUserMessageDto.getMessageType().equalsIgnoreCase(MessageTypeEnum.FILE.getName())) {
            chat.setLastMessage("[" + MessageTypeEnum.FILE.getMessage() + "]");
        } else if (updateChatUserMessageDto.getMessageType().equalsIgnoreCase(MessageTypeEnum.VOICE.getName())) {
            chat.setLastMessage("[" + MessageTypeEnum.VOICE.getMessage() + "]");
        } else if (updateChatUserMessageDto.getMessageType().equalsIgnoreCase(MessageTypeEnum.WITHDRAW.getName())) {
            chat.setLastMessage(updateChatUserMessageDto.getMessageContent());
        } else if (updateChatUserMessageDto.getMessageType().equalsIgnoreCase(MessageTypeEnum.REPLY.getName())) {
            chat.setLastMessage(StringTool.escape(updateChatUserMessageDto.getMessageContent()));
        }
        chatMapper.update(chat, new QueryWrapper<Chat>().eq("chat_id", updateChatUserMessageDto.getChatId()));
    }

    @Override
    public boolean deleteChat(DeleteChatDto deleteChatDto) {
        Channel channel = UserChannelCtxMap.getContext(deleteChatDto.getContactId());
        if (channel == null) {
            redisTemplate.opsForList().leftPush(Constant.RedisConstant.DELETE_CHAT + deleteChatDto.getContactId(), deleteChatDto.getChatId());
        } else {
            rabbitmqProducer.sendMessageProducer(
                    Constant.RabbitmqConstant.ROUTING_KEY + Constant.RabbitmqConstant.SEND_MESSAGE,
                    new MessageVo<DeleteChatUserVo>().
                            setCommandCode(WsEnum.DELETE_CHAT.getCode()).
                            setCommandName(WsEnum.DELETE_CHAT.getMessage()).
                            setContactId(deleteChatDto.getContactId()).
                            setData(
                                    new DeleteChatUserVo().
                                            setSendUserId(deleteChatDto.getUserId()).
                                            setContactId(deleteChatDto.getContactId()).
                                            setChatId(deleteChatDto.getChatId())
                            )
            );
        }
        return true;
    }

    @Override
    public boolean clearChat(DeleteChatDto deleteChatDto) {
        Channel channel = UserChannelCtxMap.getContext(deleteChatDto.getContactId());
        if (channel == null) {
            redisTemplate.opsForList().leftPush(Constant.RedisConstant.CLEAR_CHAT + deleteChatDto.getContactId(), deleteChatDto.getChatId());
        } else {
            rabbitmqProducer.sendMessageProducer(
                    Constant.RabbitmqConstant.ROUTING_KEY + Constant.RabbitmqConstant.SEND_MESSAGE,
                    new MessageVo<DeleteChatUserVo>().
                            setCommandCode(WsEnum.CLEAR_CHAT.getCode()).
                            setCommandName(WsEnum.CLEAR_CHAT.getMessage()).
                            setContactId(deleteChatDto.getContactId()).
                            setData(
                                    new DeleteChatUserVo().
                                            setSendUserId(deleteChatDto.getUserId()).
                                            setContactId(deleteChatDto.getContactId()).
                                            setChatId(deleteChatDto.getChatId())
                            )
            );
        }
        return true;
    }

}
